package com.ordering.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.ordering.constant.Constants;
import com.ordering.constant.Constants.UserType;
import com.ordering.entity.UserInfo;

/**
 *  自定义的Action，用于封装一些公共的业务逻辑和数据。
 *  我们所有的业务Action都可以继承于它，这样这些
 *  公共的信息就可以复用了。
 */
public class BaseAction extends ActionSupport implements SessionAware, ServletRequestAware,
ServletResponseAware{
    
    protected Map<String, Object> session;
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected ServletContext CONTEXT;
    private UserType userType = UserType.VISITOR;
    
    protected static String SUCCESS = "success";
    protected static String FAIL = "fail";
    
    protected static final int PAGE_SIZE = 8;
    
    @Override
    public void setSession(
            Map<String, Object> session) {
        if (null == session || session.isEmpty())
        {

            this.session = new HashMap<String, Object>();
        }
        else
        {
            this.session = session;
            Object value = session.get(Constants.SESSION_KEY_USER_TYPE);

            if (value != null)
            {
                userType = (UserType) value;
            }
        }
    }
    
    protected UserInfo getUserInfo(){
    	return (UserInfo) session.get("user");
    }
    
    
    @Override
    public void setServletRequest(HttpServletRequest request)
    {
        this.request = request;
    }

    @Override
    public void setServletResponse(HttpServletResponse response)
    {
        this.response = response;
    }

    public UserType getUserType()
    {
        return userType;
    }

    public void setUserType(UserType userType)
    {
        this.userType = userType;
    }

	public  ServletContext getCONTEXT() {
		return CONTEXT;
	}

	public  void setCONTEXT(ServletContext context) {
		CONTEXT = context;
	}
   
}
